package com.dnjn.happiness.common.config;

import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.dnjn.happiness.common.config.handlers.BaseMetaObjectHandler;
import com.dnjn.happiness.common.tenant.CustomTenantLineHandler;
import com.dnjn.happiness.common.tenant.TenantProps;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
//@MapperScan("${mybatis-plus.mapperPackage}")
public class MyBatisPlusConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.dnjn.happiness.**.mapper");
        return configurer;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(TenantProps props) {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();

        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());

        if (Boolean.TRUE.equals(props.getEnabled())) {
            TenantLineInnerInterceptor tenantLineInnerInterceptor = tenantLineInnerInterceptor(props);
            mybatisPlusInterceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        }

//        mybatisPlusInterceptor.addInnerInterceptor(new PlusDataPermissionInterceptor());
        return mybatisPlusInterceptor;
    }

    @Bean
    public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProps props) {

        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
        TenantLineHandler tenantLineHandler = new CustomTenantLineHandler(props);
        tenantLineInnerInterceptor.setTenantLineHandler(tenantLineHandler);
        return tenantLineInnerInterceptor;
    }
    /**
     * 分页插件，自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor interceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        interceptor.setMaxLimit(-1L);
//        interceptor.setOptimizeJoin(true);
        // 分页合理化
//        interceptor.setOverflow(true);
        return interceptor;
    }

    /**
     * 元对象字段填充控制器
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new BaseMetaObjectHandler();
    }

    /**
     * 使用网卡信息绑定雪花生成器
     * 防止集群雪花ID重复
     */
    @Bean
    public IdentifierGenerator idGenerator() {
        return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
    }
}